//0. 解决长按时出现浏览器打开问题
document.oncontextmenu=function(e){
        e.preventDefault();
       };

//1. 设置默认年级
var setDefaultGrade = function(){
    $.ajax({
        type: "POST",
        url: "../api/default/setDefaultGrade",
        data:{
            gradeId: gradeId
        },
        contentType: "application/json",
        success: function (r) {
            if (r.code === 0) {
                console.log("设置默认年级")
                console.log(r)
            } else {
                $.toast(r.msg, "text");
            }
        }
    });
}

//2. 根据年级科目获取所有教版信息
var getVersion = function(){
    $.ajax({
        type: "POST",
        url: "../api/home/getVersionList",
        data:{
            gradeId: gradeId,
            subjectId: subjectId
        },
        contentType: "application/json",
        success: function (r) {
            if (r.code === 0) {
                console.log("根据年级科目获取所有教版信息");
                console.log(r);
                //填充教版信息
                /* <![CDATA[ */
                var versionList = r.versionList;
                var html = '';
                for (var i = 0; i < versionList.length; i++) {
                    html +='<option value="'+versionList[i].id+'">'+versionList[i].name+'</option>'
                }
                /* ]]> */
                $("#showVersion").append(html);
                getDefaultVersion();
            } else {
                $.toast(r.msg, "text");
            }
        }
    });
}

//3. 获取默认的教版
var getDefaultVersion = function(){
    $.ajax({
        type: "POST",
        url: "../api/default/getDefaultVersion",
        data:{
            gradeId: gradeId,
            subjectId: subjectId
        },
        contentType: "application/json",
        success: function (r) {
            if (r.code === 0) {
                console.log("获取默认的教版");
                console.log(r);
                if(r.defaultVersionId){
                    $("#showVersion").val(r.defaultVersionId);
                }

                getVideoList($("#showVersion").val());
            } else {
                $.toast(r.msg, "text");
            }
        }
    });
}

//4. 根据年级科目教版获取视频列表
var getVideoList = function(versionId){
    $.ajax({
        type: "POST",
        url: "../api/video/getVideoList",
        data:{
            gradeId: gradeId,
            subjectId: subjectId,
            versionId: versionId
        },
        contentType: "application/json",
        success: function (r) {
            if (r.code === 0) {
                console.log("获取视频列表");
                console.log(r);
                var book = r.book;
                var videoList = r.videoList;
                //设置年级科目教版册别
                if(book){
		            $("title").html(book.grade.name+book.subject.name);
                }
                if (!r.videoList) {
		            $("#msg").html('<i class="icon icon-40 f20 f-green"></i>您查询的视频不存在，请重新查询');
                    $('#msg3').fadeIn();
                }else{
		            $('#msg3').fadeOut();
                    fillVideoData(videoList);
                }
	    } else {
                $.toast(r.msg, "text");
            }
        }
    });
}

var fillVideoData = function(videoList){
    var html = '';
    videoList.forEach(function(value,index,array){
        html +='<a class="weui-cell weui-cell_access" href="./video_detail.html?videoId='+value.id+'">';
        html +='<div class="weui-cell__hd"><p class="page-no"><b>'+value.pageNo+'</b><span style="font-size:10px">页</span></p></div>';
	html +='<div class="weui-cell__bd">';
        html +='<p >'+value.videoName+'</p>';
        html +='<p style="font-size: 13px;color: #888888;">'+value.unitName+'</p>';
        html +='</div>';
        html +='<div class="weui-cell__ft"></div>';
        html +='</a>';
    });
    $("#showVideoList").html(html);
}

//设置用户基本信息
var setUserBaseInfo = function (versionId) {
    $.ajax({
        type: "POST",
        url: "../api/default/setInfo",
        data: {
            gradeId: gradeId,
            subjectId: subjectId,
            versionId: versionId
        },
        contentType: "application/json",
        success: function (r) {
            if (r.code === 0) {
                console.log("设置用户默认信息")
                console.log(r)
            } else {
                $.toast(r.msg, "text");
            }
        }
    });
}

//切换教版信息
$(document).on("change","#showVersion",function () {
    setUserBaseInfo($("#showVersion").val());
    getVideoList($("#showVersion").val());
});

